package com.hzy.quetosys.controller;

import com.github.pagehelper.PageInfo;
import com.hzy.quetosys.entity.Product;
import com.hzy.quetosys.entity.ProductType;
import com.hzy.quetosys.service.ProductService;
import com.hzy.quetosys.service.ProductTypeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ProductController {
    private static final Logger logger= LoggerFactory.getLogger(ProductController.class);
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductTypeService productTypeService;
    @GetMapping("/product/produce_add")
    public ModelAndView produceAddView(){
        ModelAndView mav=new ModelAndView("product/produce_add");
       List<ProductType> productTypes= productTypeService.findAll();
        mav.addObject("productTypes",productTypes);
        return mav;
    }
    @GetMapping("/product/produce_list")
    public ModelAndView produceListView(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "keyword",required = false) String keyword
    ){
        ModelAndView mav=new ModelAndView("product/produce_list");

        PageInfo<Product> page=productService.findByKeyword(pageNum,pageSize,keyword);
        mav.addObject("page",page);
//        mav.addObject(page);
        return mav;
    }
    @GetMapping("/product/produce_modify")
    public ModelAndView produceModifyView(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "id" ,required = true) int id,
            @RequestParam(name = "keyword",required = false) String keyword
    ){
        ModelAndView mav=new ModelAndView("product/produce_modify");
        Product product=productService.findById(id);
        mav.addObject("product",product);
        return mav;
    }
    @PostMapping("/product/produce_add")
    public ModelAndView produceAdd(@Validated Product product, BindingResult bindingResult){

        ModelAndView mav=new ModelAndView("redirect:/product/produce_add");
        Map<String,String> errorMessages=new HashMap<>();
        if(bindingResult.hasErrors()){
            final List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            for(FieldError error:fieldErrors){
                errorMessages.put(error.getField(),error.getDefaultMessage());
            }
            mav.addObject("errorMessages",errorMessages);
            mav.addObject("product",product);

            mav.setViewName("product/produce_add");
        }else {
            boolean result= productService.append(product);
            if(result){
                logger.info("新增客户信息成功-"+product);
            }else {
                logger.info("新增客户信息失败-"+product);
            }
        }

        return mav;
    }
    @PostMapping("/product/produce_modify")
    public ModelAndView produceModify(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "keyword",required = false) String keyword,
            Product product
    ) throws UnsupportedEncodingException {
        String redirectUrl="redirect:/product/produce_list?pageNum="+pageNum+"&pageSize="+pageSize+"&keyword="+ URLEncoder.encode(keyword,"utf-8");
        ModelAndView mav=new ModelAndView(redirectUrl);
        boolean result=productService.modify(product);
        return mav;
    }
    @GetMapping("/product/produce_remove")
    public ModelAndView produceRemove(
            @RequestParam(name = "pageNum",required = true,defaultValue = "1") int pageNum,
            @RequestParam(name = "pageSize",required = true,defaultValue = "5") int pageSize,
            @RequestParam(name = "id" ,required = true) int id,
            @RequestParam(name = "keyword",required = false) String keyword
    ) throws UnsupportedEncodingException {
        String redirectUrl="redirect:/product/produce_list?pageNum="+pageNum+"&pageSize="+pageSize+"&keyword="+ URLEncoder.encode(keyword,"utf-8");
        ModelAndView mav=new ModelAndView(redirectUrl);
        boolean result=productService.remove(id);
        return mav;
    }
}
